package com.pyb.java.network.interceptor;

import android.os.Build;
import android.text.TextUtils;

import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.commonutil.utils.DeviceUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Params :统一添加请求头
 * @Author :zhaohuiming
 * @Date :2019/12/10
 */
public class HeadersInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        // 新的请求
        Request.Builder requestBuilder = request.newBuilder();

        requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8");

        //添加公共参数,添加到header中
        if (CacheUserInfo.getInstance().isLogin() &&
                !TextUtils.isEmpty(CacheUserInfo.getInstance().getToken()))
            requestBuilder.header("token", CacheUserInfo.getInstance().getToken());
        //app版本号
        requestBuilder.header("appversion", DeviceUtils.getVersion());
        //设备型号
        requestBuilder.header("deviceModel", DeviceUtils.getDeviceType());
        //系统版本号
        requestBuilder.header("operationSysVersion", DeviceUtils.getOsVersion());
        //品牌名称
        requestBuilder.header("deviceBrand", Build.MANUFACTURER);
        //设备操作系统
        requestBuilder.header("operationSys", "android");
        //设备唯一标识
        requestBuilder.header("deviceId", DeviceUtils.getDeviceId());

        request = requestBuilder.build();
        return chain.proceed(request);
    }
}
